package com.mingte.manghe.support.controller;

import com.alibaba.fastjson.JSON;
import com.mingte.manghe.support.service.SysGeneratorService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ：luxinsheng
 * @Email ：282860642@qq.com
 * @date ：Created in 2021/12/1 17:29
 * @description：代码生成请求类
 * @modified By：
 * @version: $
 */
@Controller
public class CodeGeneratorController {

    @Autowired
    private SysGeneratorService sysGeneratorService;


    @RequestMapping("/code")
    public void code(String tables, HttpServletResponse response) throws IOException {

        List<String> strings = sysGeneratorService.queryAllTableName();
        String[] tableArr = new String[]{};
        System.err.println("===========开始生成代码============");
        byte[] data = sysGeneratorService.code(strings);
        System.err.println("===========结束生成代码============");
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"java_template.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }
}
